<#assign entityName="${nameInfo.entityName?uncap_first}">
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<%@ include file="/jqueryeasyui/jquery.easyui.jsp"%>
<script type="text/javascript">
	var datamode;
	$(function() {
		$("#addform").window({
			title : "${nameInfo.entityName}信息",
			closed : true
		});

		$("#grid").datagrid({
			title : "${nameInfo.entityName}信息！",
			iconCls : "icon-aibo5",
			nowrap : true,
			autoRowHeight : false,
			collapsible : false,
			url : "${r"$"}{pageContext.request.contextPath}/${nameInfo.entityName}_queryAll.action",
			<#list columnList[0] as column>
			idField : "${column.javaFieldString}",
			</#list>
			columns : [ [<#list columns as column> {
				field : "${column.javaFieldString}",
				title : "${column.javaFieldString}"
			}<#if column_has_next>,</#if></#list>] ],
			singleSelect : true,
			toolbar : [ {
				"id" : "btnAdd",
				"text" : "添加${nameInfo.entityName}",
				iconCls : "icon-add",
				handler : function() {
					datamode="add";
					<#list columnList[1] as column>$("#${column.javaFieldString}").val("");
					</#list>
					$("#addform").window("open");
				}
			}, {
				"id" : "btnDelete",
				"text" : "删除${nameInfo.entityName}信息",
				iconCls : "icon-cut",
				handler : function() {
					var selected = $("#grid").datagrid("getSelected");
					if (selected) {
						$("#deleteDialog").dialog("open");
					}
				}
			},  {
				"id" : "btnModify",
				"text" : "修改${nameInfo.entityName}信息",
				iconCls : "icon-edit",
				handler :loadModify
			},{
				"id" : "btnRefresh",
				"text" : "刷新",
				iconCls : "icon-reload",
				handler : function() {
					$("#grid").datagrid("reload");
				}
			} ]
		});

		$("#btnAddData").click(function() {
			if ($("#addform").form("validate") && "add"==datamode) {
				$.post("${r"$"}{pageContext.request.contextPath}/${nameInfo.entityName}_save.action", {
					<#list columnList[1] as column>"${entityName}.${column.javaFieldString}" : $("#${column.javaFieldString}").val()<#if column_has_next>,</#if>
					</#list>
				}, showMessage, "json");
			}else if ($("#addform").form("validate") && "modify"==datamode) {
				$.post("${r"$"}{pageContext.request.contextPath}/${nameInfo.entityName}_modify.action", {
					<#list columns as column>"${entityName}.${column.javaFieldString}" : $("#${column.javaFieldString}").val()<#if column_has_next>,</#if>
					</#list>
				}, showMessage, "json");
			}
		});

		$("#deleteDialog").dialog({
			buttons : [ {
				text : "确定",
				handler : deleteData
			}, {
				text : "取消",
				handler : function() {
					$("#deleteDialog").dialog("close");
				}
			} ],
			closed : true
		});

		function deleteData() {
			var selected = $("#grid").datagrid("getSelected");
			$.post("${r"$"}{pageContext.request.contextPath}/${nameInfo.entityName}_delete.action", {
				<#list columnList[0] as column>
					"${entityName}.${column.javaFieldString}" : selected.${column.javaFieldString}
				</#list>
			}, showMessage, "json");
		}
		
		function loadModify(){
			datamode="modify";
			var selected = $("#grid").datagrid("getSelected");
			if (selected) {
				$.post("${r"$"}{pageContext.request.contextPath}/${nameInfo.entityName}_queryByKey.action",{
					<#list columnList[0] as column>
						"${entityName}.${column.javaFieldString}" : selected.${column.javaFieldString}
					</#list>
				},function(data){
					<#list columns as column>
					$("#${column.javaFieldString}").val(data.data.${column.javaFieldString});
					</#list>
					$("#addform").window("open");
				},"json");
			}
		}
		
		function showMessage(data){
			if (data.success) {
				$("#grid").datagrid("reload");
			}
			$("#deleteDialog").dialog("close");
			$("#addform").window("close");
			$.messager.show({
				"title" : "信息",
				"msg" : data.message
			});
		}

	});
</script>
</head>
<body>
	<div id="grid"></div>
	<form id="addform" onsubmit="return false;" method="post">
		<#list columnList[0] as column>
			<input type="hidden" id="${column.javaFieldString}">
		</#list>
		<table>
			<#list columnList[1] as column>
			<tr>
				<td>${column.javaFieldString}:</td>
				<td><input type="text" id="${column.javaFieldString}" class="easyui-validatebox"
					data-options="required:true"></td>
			</tr>
			</#list>
			<tr>
				<td colspan="2" align="center"><input type="button" value="保存"
					id="btnAddData"></td>
			</tr>
		</table>

		<div id="deleteDialog" title="删除记录">
			<p>是否删除选中的记录？</p>
		</div>
	</form>
</body>
</html>